app.controller('shopAddCtrl',['$scope','$timeout','$state','$cookies','shopService','utilService','toaster','$modal',function($scope,$timeout,$state,$cookies,shopService,utilService,toaster,$modal){
    $scope.systemUserAddVO={gender:0};
    $scope.shopAddVO={
        isMail:0,
        isShop:0,
        isDelivery:0,
        isEat:0,
        isReserve:0,
        isCustomGoods:0
    };

    $scope.failSend=false;

    $scope.sendAuthCode=function(){
        $scope.failSend=false;

        shopService.sendAuthCode($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.sendSuccess=true;
                    $scope.timeing();
                    break;
                case 602:
                case 680:
                    $scope.failSend=true;
                    toaster.pop('waitting', "提示", result.message);
                    break;
                default :
                    $scope.failSend=true;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.description='发送验证码';

    $scope.sendSuccess=false;
    var second=60;
    var timer;
    $scope.timeing=function(){
        if(second==0){
            second=60;
            $scope.description='发送验证码';
            $scope.sendSuccess=false;
            $timeout.cancel(timer);
            return;
        }

        timer = $timeout(function(){
            second--;
            $scope.description=second+"s后重发";
            $scope.timeing();
        },1000,true);
    }

    //判断是否已经选择服务类型
    $scope.check=function(){
        if($scope.shopAddVO.isShop==0 && $scope.shopAddVO.isMail == 0 && $scope.shopAddVO.isDelivery == 0 &&
            $scope.shopAddVO.isEat == 0 && $scope.shopAddVO.isReserve == 0 ){
            return true;
        }else{
            return false;
        }
    };

    //查询行业商品
    $scope.getIndustryFullName=function(){
        shopService.getIndustryFullName($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.industry=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.getIndustryFullName();


    //归属地区
    $scope.shop={};
    $scope.selectCounty=null;
    $scope.selectTown=null;
    $scope.selectProvice="省";
    $scope.selectTown="市";
    $scope.selectCounty="区";
    $scope.showState=false;
    $scope.towns=[];
    $scope.countys=[];
    $scope.currentState=0;
    $scope.steps={percent:20, step1:true, step2:false, step3:false};

    //点击选择框执行的方法
    $scope.findProvinces=function(level,parentId){
        $scope.showState=!$scope.showState;
        if($scope.showState){
            $scope.showState=true;
            $scope.findArea(level,parentId);
        }
    };
    //点击省之后执行的方法
    $scope.findTown=function(level,item){
        $scope.findArea(level,item.cityId,item.name);
        $scope.shopAddVO.areaProvinceId=item.id;
    };
    //点击市之后执行的方法
    $scope.findCounty=function(level,item){
        $scope.findArea(level,item.cityId,item.name);
        $scope.shopAddVO.areaCityId=item.id;
    };
    //点击区之后执行的方法
    $scope.CountyHide=function(item){
        $scope.selectCounty=item.name;
        $scope.shopAddVO.areaCountyId=item.id;
        $scope.showState=false;
        $scope.currentState=3;
    };
    //发送并处理service传回来的数据
    $scope.findArea=function(level,parentId,name){
        shopService.findArea($scope,level,parentId,name).then(function(result){
            switch (result.status){
                case 200:
                    //请求level=0时，返回的是省的区域
                    if(level==0){
                        $scope.provinces=result.data;
                        //请求level=1时，返回的是市的区域
                    }else if(level==1){
                        $scope.towns=result.data;
                        $scope.selectProvice=name;
                        $scope.currentState=1;
                        $scope.selectTown="市";
                        $scope.selectCounty="区";
                        $scope.steps.step2=true;
                    }else if(level==2){
                        //请求level=2时，返回的是区的区域
                        $scope.countys=result.data;
                        $scope.selectTown=name;
                        $scope.steps.step3=true;
                        $scope.currentState=2;
                        $scope.selectCounty="区";
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //套餐选择
    $scope.setMealId=null;

    //查询套餐信息
    $scope.setMealVO={type:0};

    $scope.loading=false;

    $scope.getSetMealList = function(){

        if($scope.loading){
            return;
        }
        $scope.loading=true;

        shopService.getSetMealList($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.loading=false;
                    $scope.setMeal = result.data;
                    break;
                default :
                    $scope.loading=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });
    };

    $scope.getSetMealList();

    $scope.chooseSetMeal=function(id,index){
        $scope.setMealId=id;
        $scope.index=index;
    };

    //添加店铺
    $scope.addShop=function(){
        shopService.addShop($scope).then(function(result){
            switch (result.status){
                case 200:
                    $state.go('app.shop');
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 699:
                case 647:
                case 408:
                case 602:
                case 648:
                    toaster.pop('wait', "提示",result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.open=function(){
        if($scope.shopAddVO.isReserve==1){
            $scope.shopAddVO.isCustomGoods=1;
        }
    }
}]);



app.controller('shopCtrl',['$scope','$state','$cookies','shopService','utilService',
    function($scope,$state,$cookies,shopService,utilService){

    //查询商品列表
    $scope.findShopList=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + '/shop/chain/list'
        }).then(function(result){
            console.log(result);
        });
    };

    $scope.findShopList();

}]);



app.controller('shopEditCtrl',['$scope','$timeout','$state','$cookies','shopService','utilService','toaster','$stateParams',
    function($scope,$timeout,$state,$cookies,shopService,utilService,toaster,$stateParams){
    $scope.shopEditVO={
        id:$stateParams.id,
        isMail:0,
        isShop:0,
        isDelivery:0,
        isEat:0,
        isReserve:0,
        isCustomGoods:0
    };

    //查询该店铺信息
    $scope.queryShop=function(){
        shopService.queryShop($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.shopEditVO=result.data;
                    delete $scope.shopEditVO.lat;
                    delete $scope.shopEditVO.lng;
                    $scope.shopEditVO.id=$stateParams.id;
                    $scope.selectProvice=$scope.shopEditVO.provinceName;
                    $scope.selectTown=$scope.shopEditVO.cityName;
                    $scope.selectCounty=$scope.shopEditVO.countyName;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    $scope.queryShop();


    //判断是否已经选择服务类型
    $scope.check=function(){
        if($scope.shopEditVO.isShop==0 && $scope.shopEditVO.isMail == 0 && $scope.shopEditVO.isDelivery == 0 &&
            $scope.shopEditVO.isEat == 0 && $scope.shopEditVO.isReserve == 0 ){
            return true;
        }else{
            return false;
        }
    };

    //查询行业
    $scope.getIndustryFullName=function(){
        shopService.getIndustryFullName($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.industry=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.getIndustryFullName();


    //归属地区
    $scope.shop={};
    $scope.selectCounty=null;
    $scope.selectTown=null;
    $scope.selectProvice="省";
    $scope.selectTown="市";
    $scope.selectCounty="区";
    $scope.showState=false;
    $scope.towns=[];
    $scope.countys=[];
    $scope.currentState=0;
    $scope.steps={percent:20, step1:true, step2:false, step3:false};

    //点击选择框执行的方法
    $scope.findProvinces=function(level,parentId){
        $scope.showState=!$scope.showState;
        if($scope.showState){
            $scope.showState=true;
            $scope.findArea(level,parentId);
        }
    };
    //点击省之后执行的方法
    $scope.findTown=function(level,item){
        $scope.findArea(level,item.cityId,item.name);
        $scope.shopEditVO.areaProvinceId=item.id;
    };
    //点击市之后执行的方法
    $scope.findCounty=function(level,item){
        $scope.findArea(level,item.cityId,item.name);
        $scope.shopEditVO.areaCityId=item.id;
    };
    //点击区之后执行的方法
    $scope.CountyHide=function(item){
        $scope.selectCounty=item.name;
        $scope.shopEditVO.areaCountyId=item.id;
        $scope.showState=false;
        $scope.currentState=3;
    };

    //发送并处理service传回来的数据
    $scope.findArea=function(level,parentId,name){
        shopService.findArea($scope,level,parentId,name).then(function(result){
            switch (result.status){
                case 200:
                    //请求level=0时，返回的是省的区域
                    if(level==0){
                        $scope.provinces=result.data;
                        //请求level=1时，返回的是市的区域
                    }else if(level==1){
                        $scope.towns=result.data;
                        $scope.selectProvice=name;
                        $scope.currentState=1;
                        $scope.selectTown="市";
                        $scope.selectCounty="区";
                        $scope.steps.step2=true;
                    }else if(level==2){
                        //请求level=2时，返回的是区的区域
                        $scope.countys=result.data;
                        $scope.selectTown=name;
                        $scope.steps.step3=true;
                        $scope.currentState=2;
                        $scope.selectCounty="区";
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //查询该店铺信息
    $scope.editShop=function(){
        shopService.editShop($scope).then(function(result){
            switch (result.status){
                case 200:
                    $state.go('app.shop');
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };



}]);